<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .container {
        width: 520px;
        height: 280px;
        border: 1px solid;
        position: relative;
        margin: 0 auto;
        overflow: hidden;
      }

      .imgContainer {
        width: 2600px;
        height: 280px;
        position: absolute;
        left: 0px;
      }

      .imgContainer img {
        float: left;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="imgContainer">
        <img src="../images/1.png" alt="" />
        <img src="../images/2.png" alt="" />
        <img src="../images/3.png" alt="" />
        <img src="../images/4.png" alt="" />
        <img src="../images/5.png" alt="" />
      </div>
    </div>
    <script>
      var imgContainerEle = document.querySelector(".imgContainer");
      var page = 1;
      setInterval(function () {
        move(imgContainerEle, "left", -520 * page, function () {
          page++;
          console.log(page);
          if (page == 6) {
            page = 1;
            imgContainerEle.style.left = "0px";
          }
        });
      }, 2000);
      function cloneImg() {
        imgContainerEle.style.width = "3120px";
        var fImg = imgContainerEle.firstElementChild;
        var fCopyImg = fImg.cloneNode(true);
        imgContainerEle.appendChild(fCopyImg);
      }
      cloneImg();
      function move(boxEle, dir, dis, cb) {
        function fn() {
          var num = parseInt(getComputedStyle(boxEle)[dir]);
          var res = dis > num ? 1 : -1;
          var speed = 20 * res;
          boxEle.style[dir] = num + speed + "px";
          if (num + speed == dis) {
            cb && cb();
          } else {
            window.requestAnimationFrame(fn);
          }
        }
        fn();
      }
    </script>
  </body>
</html>
